package com.bsoft.gol.hbs.service.impl;

import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import com.bsoft.gol.hbs.dao.ReviewOpinionTemplateDAO;
import com.bsoft.gol.hbs.dao.ReviewOpinionTypeDAO;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.bean.consultation.OpinionListVo;
import pcn.bean.consultation.OpinionTemplateVo;
import pcn.consultation.ReviewOpinionTemplate;
import pcn.consultation.ReviewOpinionType;
import pcn.service.consultation.ReviewOpinionService;
import com.bsoft.gol.hbs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SsdevService("reviewOpinionService")
public class ReviewOpinionServiceImpl implements ReviewOpinionService {
    @Autowired
    private ReviewOpinionTemplateDAO reviewOpinionTemplateDAO;

    @Autowired
    private ReviewOpinionTypeDAO reviewOpinionTypeDAO;
    /**
     * 机构查询审核意见
     * @param orgId
     * @return
     */
    @RpcService
    @Override
    public List<OpinionListVo> findOpinions(String orgId) {
        List<OpinionListVo> opinionListVoList =new ArrayList<>();
        List<ReviewOpinionType> opinionTypes = reviewOpinionTypeDAO.findOpinionTypebyOrgId(orgId);
        if(CommonUtils.isEmpty(opinionTypes)){
            return null;
        }

        for (ReviewOpinionType reviewOpinionType:opinionTypes) {
            OpinionListVo opinionListVo = new OpinionListVo();
            opinionListVo.setOpinionTypeId(reviewOpinionType.getOpinionTypeId());
            opinionListVo.setOrgId(reviewOpinionType.getOrgId());
            opinionListVo.setTypeTitle(reviewOpinionType.getTypeTitle());
            List<OpinionTemplateVo> opinionTemplateVos = new ArrayList<>();
            List<ReviewOpinionTemplate> reviewOpinionTemplates = reviewOpinionTemplateDAO.findOpinionTemplatebytypeId(reviewOpinionType.getOpinionTypeId());
            if(CommonUtils.isEmpty(reviewOpinionTemplates)){
                opinionListVoList.add(opinionListVo);
                continue;
            }
            for (ReviewOpinionTemplate reviewOpinionTemplate:reviewOpinionTemplates) {
                OpinionTemplateVo opinionTemplateVo = new OpinionTemplateVo();
                opinionTemplateVo.setOpinionTemplateId(reviewOpinionTemplate.getOpinionTemplateId());
                opinionTemplateVo.setOpinionTemplateTitle(reviewOpinionTemplate.getOpinionTemplateTitle());
                opinionTemplateVo.setLastUseTime(reviewOpinionTemplate.getLastUseTime());
                opinionTemplateVo.setNumberOfUse(reviewOpinionTemplate.getNumberOfUse());
                opinionTemplateVos.add(opinionTemplateVo);
            }
            opinionListVo.setOpinionTemlateList(opinionTemplateVos);
            opinionListVoList.add(opinionListVo);
        }
        return opinionListVoList;
    }

    /**
     * 保存意见
     * @param reviewOpinionTemplate
     */
    @RpcService
    public void addOpinionTemplate(ReviewOpinionTemplate reviewOpinionTemplate){
        if(reviewOpinionTemplate.getOpinionTemplateId() == 0){
            reviewOpinionTemplate.setGmtCreate(new Date());
            reviewOpinionTemplate.setGmtModified(new Date());
            reviewOpinionTemplate.setNumberOfUse(0);
            reviewOpinionTemplateDAO.save(reviewOpinionTemplate);
        }else{
            reviewOpinionTemplate.setGmtModified(new Date());
            reviewOpinionTemplateDAO.update(reviewOpinionTemplate);
        }
    }

    /**
     * 保存类型
     * @param reviewOpinionType
     */
    @RpcService
    public void addOpinionType(ReviewOpinionType reviewOpinionType){
        if(reviewOpinionType.getOpinionTypeId() == 0){
            reviewOpinionType.setGmtCreate(new Date());
            reviewOpinionType.setGmtModified(new Date());
            reviewOpinionTypeDAO.save(reviewOpinionType);
        }else{
            reviewOpinionType.setGmtModified(new Date());
            reviewOpinionTypeDAO.update(reviewOpinionType);
        }
    }

}
